package com.lxc.demos.util.c2dm;


import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

/**
 * Utilities for device registration. Will keep track of the registration token
 * in a private preference. 和注册相关的一些实用函数
 * 
 * @author Shineflag
 * 
 */
public class C2DMessaging {
	public static final String EXTRA_SENDER = "sender";
	public static final String EXTRA_APPLICATION_PENDING_INTENT = "app";
	public static final String REQUEST_REGISTRATION_INTENT = "com.google.android.c2dm.intent.REGISTER";
	public static final String REQUEST_UNREGISTRATION_INTENT = "com.google.android.c2dm.intent.UNREGISTER";

	public static final String LAST_REGISTRATION_CHANGE = "last_registration_change";
	public static final String BACKOFF = "backoff";
	/* GSF为GoogleServicesFramework.apk的缩写 */
	/* 不是很明白 */
	public static final String GSF_PACKAGE = "com.google.android.gsf";

	// package
	/* 用来获取SharedPreferences */
	static final String PREFERENCE = "com.google.android.c2dm";

	private static final long DEFAULT_BACKOFF = 30000;

	/**
	 * Initiate c2d messaging registration for the current application
	 * 
	 * @param context
	 * @param emailOfSender
	 *            发送者帐号（通常为邮箱名）
	 */
	public static void register(Context context, String emailOfSender) {
		Log.d(C2DMConfig.TAG,"C2DMessaging.register");
		Intent registrationIntent = new Intent(REQUEST_REGISTRATION_INTENT);

		registrationIntent.setPackage(GSF_PACKAGE);// 作用不是很明白

		registrationIntent.putExtra(EXTRA_SENDER, emailOfSender);// 发送者的帐号，通常
																	// 为邮箱名
		registrationIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT,
				PendingIntent.getBroadcast(context, 0, new Intent(), 0));
		context.startService(registrationIntent);

		// TODO: if intent not found, notification on need to have GSF

	}

	/**
	 * Unregister the application. New messages will be blocked by server.
	 * 
	 * @param context
	 */
	public static void unregister(Context context) {
		Intent unregIntent = new Intent(REQUEST_UNREGISTRATION_INTENT);

		unregIntent.setPackage(GSF_PACKAGE);// 不是很明白其意思

		unregIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT,
				PendingIntent.getBroadcast(context, 0, new Intent(), 0));
		context.startService(unregIntent);
	}

	/**
	 * Return the current registration id.
	 * 
	 * If result is empty,the registration has failed.
	 * 
	 * @param context
	 * 
	 * @return registration id, or empty string if the registration is not
	 *         complete.
	 */
	public static String getRegistrationId(Context context) {
		final SharedPreferences prefs = context.getSharedPreferences(
				PREFERENCE, Context.MODE_PRIVATE);
		String registrationId = prefs.getString("dm_registration", "");
		return registrationId;

	}

	public static long getLastRegistrationChange(Context context) {
		final SharedPreferences prefs = context.getSharedPreferences(
				PREFERENCE, Context.MODE_PRIVATE);
		return prefs.getLong(LAST_REGISTRATION_CHANGE, 0);
	}

	//package
	static long getBackoff(Context context) {
		final SharedPreferences prefs = context.getSharedPreferences(
				PREFERENCE, Context.MODE_PRIVATE);
		return prefs.getLong(BACKOFF, DEFAULT_BACKOFF);
	}

	//package
	static void setBackoff(Context context, long backoff) {
		final SharedPreferences prefs = context.getSharedPreferences(
				PREFERENCE, Context.MODE_PRIVATE);
		Editor editor = prefs.edit();
		editor.putLong(BACKOFF, backoff);
		editor.commit();
	}

	//package
	static void clearRegistrationId(Context context) {
		final SharedPreferences prefs = context.getSharedPreferences(
				PREFERENCE, Context.MODE_PRIVATE);
		Editor editor = prefs.edit();
		editor.putString("dm_registration", "");
		editor.putLong(LAST_REGISTRATION_CHANGE, System.currentTimeMillis());
		editor.commit();
	}
	
	//package
	static void setRegistrationId(Context context,String registrationId){
		final SharedPreferences prefs = context.getSharedPreferences(PREFERENCE, Context.MODE_PRIVATE);
		Editor editor = prefs.edit();
        editor.putString("dm_registration", registrationId);
        editor.commit();
	}

}
